package xin.yangshuai.activemqgeneral.jms.queueacknowledge;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * JmsProducer
 *
 * @author shuai
 * @date 2020/6/22
 */
public class JmsProducer {

    public static final String ACTIVEMQ_URL = "tcp://192.168.124.7:61616";
    public static final String QUEUE_NAME = "queue01-acknowledge";

    public static void main(String[] args) throws JMSException, InterruptedException {

        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);

        Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();

        //开启事务
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

        MessageProducer messageProducer = session.createProducer(session.createQueue(QUEUE_NAME));

        for (int i = 0; i < 30; i++) {
            //textMessage
            String message = "msg---" + (i + 1);
            TextMessage textMessage = session.createTextMessage(message);
            System.out.println("****生产者1生产消息-TextMessage：" + message);
            messageProducer.send(textMessage);
        }

        messageProducer.close();
        session.close();
        connection.close();

        System.out.println("*****消息发布到MQ完成");
    }
}
